<template>
  <div>
    <el-dialog v-model="dialogVisible" width="80%" :close-on-click-modal="false" align-center :show-close="false"
      :before-close="handleClose" destroy-on-close draggable>
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>论文{{ ruleForm.id ? '编辑' : '新增' }}</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close">
            <CircleCloseFilled color="#fff" />
          </el-icon>
        </div>
      </template>
      <!-- 步骤条 -->
      <div class="pt-[30px] pb-[20px] bg-white">
        <el-steps :active="activeStep" align-center v-if="!ruleForm.id">
          <el-step title="1.基本信息" />
          <el-step title="2.依托项目" />
          <el-step title="3.论文收录" />
          <el-step title="4.完成登记" />
        </el-steps>
        <el-tabs v-model="activeStep" class="demo-tabs" v-else @tab-change="handleClick">
          <el-tab-pane label="基本信息" :name="0"></el-tab-pane>
          <el-tab-pane label="依托项目" :name="1"></el-tab-pane>
          <el-tab-pane label="转载情况" :name="4"></el-tab-pane>
          <el-tab-pane label="论文收录" :name="2"></el-tab-pane>
        </el-tabs>
      </div>
      <basicInfo v-show="activeStep === 0" :zcgsdwList="zcgsdwList" :sydwList="sydwList" :xlList="xlList"
        :zcList="zcList" ref="basicInfoRef" :lwlx="lwlx" :jbList="jbList" :lbList="lbList" :xmlyList="xmlyList"
        :fbfwList="fbfwList" :kjxkflList="kjxkflList" :skxkflList="skxkflList" :lwzzList="lwzzList"
        @closeDiglog="closeDiglog" @nextStepMet="nextStepMet" @endLoading="endLoading" @startLoading="loading = true;">
      </basicInfo>
      <supportProject v-if="activeStep === 1" ref="supportProjectRef" :userList="userList" type="paper"
        :productId="productId" @closesupport="closesupport">
      </supportProject>
      <inclusionPaper v-if="activeStep === 2" :productId="productId"></inclusionPaper>
      <completePage v-show="activeStep === 3"></completePage>
      <situationReproduction v-if="activeStep === 4" :productId="productId"></situationReproduction>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="previousStep()" v-if="!ruleForm.id && activeStep > 0">上一步</el-button>
            <el-button type="primary" @click="temporarilyStore" v-if="activeStep == 0 && ruleForm.checkstatus != '2'"
              :loading="loading">暂存</el-button>
            <el-button type="primary" @click="save()" :loading="loading" v-if="checkButton()">{{ activeStep !== 3 ?
      '下一步' : Cookies.get("role-key") == 'biz_kyry' ? '提交' : '保存并通过'
              }}</el-button>
            <el-button type="primary" @click="save1()" v-if="checkEditButton()" :loading="loading">{{
      Cookies.get("role-key") == 'biz_kyry'
        ? '提交' : ruleForm.checkstatus == '2' || (ruleForm.id && activeStep != 0) ? '保存' : '保存并通过' }}</el-button>
            <el-button type="primary" @click="closeDiglog">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { nextTick } from 'vue';
import basicInfo from "./basicInfo.vue";
import supportProject from "@/views/achievement/components/supportProject.vue"
import completePage from "@/views/achievement/components/completePage.vue"
import inclusionPaper from "@/views/achievement/components/inclusionPaper.vue"
import situationReproduction from "@/views/achievement/components/situationReproduction.vue"
import Cookies from 'js-cookie'
import useUserStore from "@/store/modules/user";
import { getPersonDetails } from "@/api/achievement/common.js"
import { getpaperInfo } from "@/api/achievement/kycg";

const dialogVisible = defineModel();
const props = defineProps({
  xlList: Array,
  zcList: Array,
  zcgsdwList: Array,
  sydwList: Array,
  jbList: Array,
  xmlyList: Array,
  fbfwList: Array,
  kjxkflList: Array,
  skxkflList: Array,
  lwlx: Array,
  lbList: Array,
  lwzzList: Array,
  id: String,
})

const loading = ref(false)

const activeStep = ref(0);
const ruleForm = reactive({
  id: '',
  checkstatus: '',
});
const productId = ref('')
const userList = ref([])

const basicInfoRef = ref(null)
const supportProjectRef = ref(null)

const userStore = useUserStore();

const getFrom = async (i) => {
  await nextTick()
  for (const key in i) {
    ruleForm[key] = i[key];
  }
  basicInfoRef.value.getRuleForm(i)
  userList.value = i.authorList;
  productId.value = i.id;
}
const save = () => {
  if (activeStep.value === 0) {

    basicInfoRef.value.submitForm('-1')
  } else if (activeStep.value === 1) {
    supportProjectRef.value.relyProjectSaveMet()
  } else if (activeStep.value === 2) {
    activeStep.value++
  } else {
    getpaperInfo(productId.value).then((res) => {
      if (res.code == 200) {
        let fdInfo = {
          checkstatus: res.data.checkstatus,
          isAuditor: res.data.isAuditor
        }
        basicInfoRef.value.submitForm(undefined, '0', fdInfo)
      }
    });

    // closeDiglog()
  }
}

const temporarilyStore = () => {

  if (!ruleForm.id || Cookies.get("role-key") == 'biz_kyry') {
    basicInfoRef.value.submitForm('-1')
  } else {
    // basicInfoRef.value.submitForm('-1', '1')
    basicInfoRef.value.submitForm('-1')
  }

}
const save1 = () => {
  if (activeStep.value === 0) {

    basicInfoRef.value.submitForm()
  } else if (activeStep.value === 1) {
    supportProjectRef.value.relyProjectSaveMet(1)
  } else {
    closeDiglog()
  }
}
const previousStep = async () => {
  if (activeStep.value > 0) {
    activeStep.value--
    if (activeStep.value == 1) {
      await nextTick()
      supportProjectRef.value.initial(userList.value)
    }
  }
}
const closeDiglog = () => {

  dialogVisible.value = false
  setTimeout(() => {
    activeStep.value = 0
    ruleForm.id = ''
    delete ruleForm.checkstatus
  }, 500)
  emit('callBack', "")
}
const nextStepMet = async (i, j) => {
  userList.value = i;
  productId.value = j;
  activeStep.value = 1
  await nextTick()
  supportProjectRef.value.initial(userList.value)
}
const endLoading = () => {
  loading.value = false;
}
const closesupport = (i) => {
  if (i == '1') {
    closeDiglog()
  } else {
    activeStep.value = 2
  }

}
const handleClick = async (name) => {
  if (name == 1) {
    await nextTick()
    supportProjectRef.value.initial(userList.value)
  }
}
const emit = defineEmits(['callBack']);
const handleClose = (done) => {
  ruleForm.id = ''
  delete ruleForm.checkstatus
  activeStep.value = 0
  emit('callBack', "")
  done()
}


const checkButton = () => {
  if (!ruleForm.id) {
    return true
  } else {
    return false
  }
}

const checkEditButton = () => {
  if (ruleForm.id) {
    if (Cookies.get("role-key") == 'biz_kyry') {
      return true
    } else if (Cookies.get("role-key") == 'biz_kyms') {
      if ((ruleForm.checkstatus == '1' && ruleForm.isAuditor) || activeStep.value != 0) {
        return true
      } else {
        return false
      }
    } else if (Cookies.get("role-key") == 'biz_kycgly') {
      if (ruleForm.checkstatus == '1') {
        return ruleForm.isAuditor || activeStep.value != 0 ? true : false
      } else {
        return true
      }
    } else {
      return false
    }

  } else {
    return false
  }
}



const getKyryInfo = () => {
  if (Cookies.get("role-key") == 'biz_kyry') {
    let kyryInfo = {
      authorType: '1',
      eduLevelId: '',
      authorName: '',
      isTxAuthor: '0',
      titleId: '',
      authorUnit: '',
      authorUnitId: '',
      workRatio: null,
      personId: '',   //userId
      orderId: 1,
      authorAccount: "",   //职工号
      sexId: '',
      subjectId: "",
      unitId: '',
      unitName: '',
      disable: true
    }
    let pamrs = {
      account: userStore.account
    }
    let kyryInfoArr = []
    getPersonDetails(pamrs).then(res => {
      if (res.code == 200) {
        kyryInfoArr = res.data.filter((item) => {
          return item.name == userStore.nickName
        });
        if (kyryInfoArr && kyryInfoArr.length) {
          kyryInfo.authorName = kyryInfoArr[0].name
          kyryInfo.personId = kyryInfoArr[0].id
          kyryInfo.authorAccount = kyryInfoArr[0].account
          kyryInfo.sexId = kyryInfoArr[0].sexId
          kyryInfo.subjectId = kyryInfoArr[0].subjectId
          kyryInfo.authorUnitId = kyryInfoArr[0].unitId
          kyryInfo.authorUnit = kyryInfoArr[0].unitName
          kyryInfo.eduLevelId = kyryInfoArr[0].eduLevelId
          kyryInfo.titleId = kyryInfoArr[0].titleId
          basicInfoRef.value.getKyryInfo([kyryInfo])
        }
      }
    })
  }
}

defineExpose({
  getFrom,
  getKyryInfo
})
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>
